//
//  MQMyCustomsViewController.m
//  meiquan
//
//  Created by hejingjin on 2017/10/28.
//  Copyright © 2017年 mq. All rights reserved.
//

#import "MQMyCustomsViewController.h"
#import "MQMyAruthCell.h"
#import "MQCustomInfoViewController.h"
#import "MQBaseUserInfoModel.h"
#import "MQCustomInfo.h"

#import "MQadviews.h"
#import "MQAdmodels.h"

@interface MQMyCustomsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *mycustomstableview;

@property(nonatomic,strong)NSMutableArray *datasource;

@property(nonatomic,strong)MQadviews *adviews;

@property(nonatomic,strong)MQAdmodels *admodels;

@end

@implementation MQMyCustomsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _datasource = [NSMutableArray array];
    
    self.title = @"客户资料";
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    //
    button.bst_size = CGSizeMake(35, 35);
    // 让按钮内部的所有内容左对齐
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    
    // 让按钮的内容往左边偏移10
//    button.contentEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    [button setImage:[UIImage imageNamed:@"tianjia"] forState:UIControlStateNormal];
     [button setImage:[UIImage imageNamed:@"tianjia"] forState:UIControlStateHighlighted];
//    [button setTitle:@" 发布 " forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    [button setTitleColor:BSTColorFromRGB(0x939393) forState:UIControlStateNormal];
    [button setTitleColor:BSTColorFromRGB(0x939393) forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(addBUttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    // 修改导航栏左边的item
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    [self setUpMainInterface];
    
    
    [self getaddata];
}

-(void)getaddata{
    
    NSArray *array = @[@"page1",@"@page2",@"page3"];
    int x = arc4random() % 3;
    
    MQBaseUserInfoModel *userinfo = [MQBaseUserInfoModel shareInstant];
    [userinfo loadUserinfoFromSadeBox];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:userinfo.userId forKey:@"userId"];
    [param setValue:array[x] forKey:@"code"];
    [param setValue:@"page1" forKey:@"code"];
    
    [MQRequestTools POSTWithMethod:adv_index params:param success:^(id json) {
        
        NSDictionary *dic = [json valueForKey:@"data"];
        _admodels = [MQAdmodels mj_objectWithKeyValues:dic];
        if (_admodels.advId) {
            [_adviews.adimageviews sd_setImageWithURL:[NSURL URLWithString:_admodels.pic] placeholderImage:[UIImage imageNamed:@""]];
        }else{
            _adviews.bst_height = 0;
            _mycustomstableview.bst_y = NAVHEIGHT;
            _mycustomstableview.bst_height =BSTScreenH -NAVHEIGHT;
        }
        
        
        UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [_adviews addGestureRecognizer:tapGesturRecognizer];
        
    } failure:^(NSError *error) {
        
    }];
    
    
}

-(void)tapAction:(UITapGestureRecognizer *)obj{
    NSLog(@"%@",_admodels.url);
    MQAdDetailViewController *advc = [[MQAdDetailViewController alloc] init];
    advc.url = _admodels.url;
     advc.type = _admodels.type;
    [self.navigationController pushViewController:advc animated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_mycustomstableview.mj_header beginRefreshing];
}

-(void)addBUttonPressed:(UIButton *)sender{
    NSLog(@"1111");
    MQCustomInfoViewController *custominfovc = [[MQCustomInfoViewController alloc] init];
    custominfovc.type = 0;
    [self.navigationController pushViewController:custominfovc animated:YES];
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}
//实现此方法，会出现删除按钮
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //获取删除的好友
    MQCustomInfo *custominfo = _datasource[indexPath.row];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    [param setValue:custominfo.ID forKey:@"id"];
    
    MQBaseUserInfoModel *userinfo = [MQBaseUserInfoModel shareInstant];
    [userinfo loadUserinfoFromSadeBox];
    [param setValue:userinfo.userId forKey:@"userId"];
    
    [MQRequestTools GETWithMethod:customer_delete params:param success:^(id json) {
        [_datasource removeObject:custominfo];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        
        
    } failure:^(NSError *error) {
        
    }];
    
    
  
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _datasource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 79.f;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MQMyAruthCell *cell = [MQMyAruthCell cellWIthTableview:tableView];
    MQCustomInfo *custominfo = _datasource[indexPath.row];
    cell.weixinLabel.text = [NSString stringWithFormat:@"客户姓名: %@",custominfo.realName];
    cell.productcodeLabel.text = [NSString stringWithFormat:@"手机号: %@",custominfo.telephone];
    cell.stateLabel.text = [NSString stringWithFormat:@"地址: %@%@%@%@",custominfo.provinceName,custominfo.cityName,custominfo.areaName,custominfo.addr];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MQCustomInfo *custominfo = _datasource[indexPath.row];
    MQCustomInfoViewController *custominfovc = [[MQCustomInfoViewController alloc] init];
    custominfovc.custom = custominfo;
    custominfovc.type = 1;
    [self.navigationController pushViewController:custominfovc animated:YES];
}

-(void)setUpMainInterface{
    
    self.view.backgroundColor = BSTColorFromRGB(0xefefef);
    
    _adviews = [MQadviews loadadviews];
    _adviews.bst_x = 0;
    _adviews.bst_width = BSTScreenW;
    _adviews.bst_height = 70 *Persion;
    _adviews.bst_y = NAVHEIGHT;
    [self.view addSubview:_adviews];
    
    
    [self.view addSubview:self.mycustomstableview];
    
}

-(UITableView *)mycustomstableview{
    if (!_mycustomstableview) {
        
        
        _mycustomstableview = [[UITableView alloc] initWithFrame:CGRectMake(0, NAVHEIGHT + 70 * Persion, BSTScreenW, BSTScreenH - 70 *Persion ) style:UITableViewStylePlain];
        _mycustomstableview.delegate = self;
        _mycustomstableview.dataSource = self;
        _mycustomstableview.backgroundColor = BSTColorFromRGB(0xf5f5f5);
        _mycustomstableview.showsVerticalScrollIndicator = NO;
        UIView *footerview = [UIView new];
        _mycustomstableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        footerview.backgroundColor = BSTColorFromRGB(0xf5f5f5);
        _mycustomstableview.tableFooterView = footerview;
        
        _mycustomstableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self getcustomdata];
        }];
        [_mycustomstableview.mj_header beginRefreshing];
        
    }
    return _mycustomstableview;
}


-(void)deletecustom{
    
    
    
    
}


-(void)getcustomdata{
    MQBaseUserInfoModel *userinfo = [MQBaseUserInfoModel shareInstant];
    [userinfo loadUserinfoFromSadeBox];
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:userinfo.userId forKey:@"userId"];
    [MQRequestTools GETWithMethod:customer_list params:param success:^(id json) {
        

        NSArray *customlist = [json valueForKey:@"data"];
        
        [_datasource removeAllObjects];
        for (NSDictionary *dic in customlist) {
            MQCustomInfo *custominfo = [MQCustomInfo mj_objectWithKeyValues:dic];
            [_datasource addObject:custominfo];
            
        }
         [_mycustomstableview.mj_header endRefreshing];
        [_mycustomstableview reloadData];
        
        
    } failure:^(NSError *error) {
        
    }];
    
    
}

@end
